/*
 Copyright (c) Whitemice Consulting, 2007, 2008, 2009
 Adam Tauno Williams <awilliam@whitemice.org>
 License: MIT/X11

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Gtk;
using Whitemice.ZOGI;
using Whitemice.ZOGI.GtkWidgets;
using Whitemice.ZOGI.GtkForms;
using Whitemice.ZOGI.GtkFolios;

namespace Whitemice.ZOGI.GtkExtra
{  
  [System.ComponentModel.Category("Extra")]
  [System.ComponentModel.ToolboxItem(true)]
  public partial class EnterpriseSearch : Gtk.Bin, IZOGIWidget
  {   
		protected EnterpriseQBEDialog	                qbeDialog;
		protected Label							                  status;
		protected MultiResultCallback                 searchResultCallback;
		
		public    EntityEventHandler                  OnEnterpriseEdit;
		public    EntityEventHandler                  OnEnterpriseRemove;
		
    public EnterpriseSearch()
    { 
			this.Build();
			
			status = null;
			qbeDialog = null;
			enterpriseList.OnEnterpriseEdit = new EntityEventHandler(EnterpriseEdit);
			enterpriseList.OnEnterpriseRemove = new EntityEventHandler(EnterpriseRemove);
			searchResultCallback = new MultiResultCallback(this.ReceiveDataCallback);
			this.ShowAll();
		} // end Constructor      
		
		public bool PopupEnabled
		{
			get { return enterpriseList.PopupEnabled; }
			set { enterpriseList.PopupEnabled = value; }
		} // end PopupEnabled

		protected virtual void OnFindClicked (object sender, System.EventArgs e)
		{
      EnterpriseSearchCommand command;
      
			if (searchValue.Text.Length > 2)
			{
        command = new EnterpriseSearchCommand("EnterpriseSearch");
        command.AddORCriteria("description",
                              string.Format("*{0}*", searchValue.Text),
                              true,
                              false);
				Query(command);
			}		
		} // end OnFindClicked

		protected virtual void OnQBEClicked (object sender, System.EventArgs e)
		{
			if (qbeDialog == null)
			{
				qbeDialog = new EnterpriseQBEDialog(
					null, 
					Gtk.DialogFlags.DestroyWithParent);
				qbeDialog.OnQBEDialogOk += new System.EventHandler(OnQBEOk);
				qbeDialog.OnQBEDialogCancel += new System.EventHandler(OnQBECancel);
				qbeDialog.Destroyed += new System.EventHandler(OnQBEDestroyed);
			} else
				{
					qbeDialog.Show();
					qbeDialog.Present();
				}		
		} // end OnQbeClicked
    
		protected virtual void OnQBEOk(object sender, EventArgs args)
		{
			qbeDialog.Hide();
			Query(qbeDialog.GetQuery(65535, "EnterpriseSearch"));
		} // end OnQBEOk
		
		protected virtual void OnQBECancel(object sender, EventArgs args)
		{
			qbeDialog.Hide();
		} // end OnQBECancel
		
		protected virtual void OnQBEDestroyed(object sender, EventArgs args)
		{
			qbeDialog = null;
		} // end OnQBEDestroyed
		
		protected virtual void Query(IZOGICommand criteria)
		{
      Server.Connection.RunCommand(criteria);
      criteria.Callback = searchResultCallback;
		  SetStatus("Enterprise search dispatched.");
		} // end Query
		
		protected void EnterpriseEdit(
			Entity entity, 
			object context, 
			Exception exception)
		{
      if (OnEnterpriseEdit == null)
      {
        this.ZLOG("No OnEnterpriseEdit delegate established");
        return;
      }
      if (entity == null)
      {
        this.ZLOG("Edit delegate invoked with null entity");
        return;
      }
      OnEnterpriseEdit(entity, context, exception); 
    }
		
		protected void EnterpriseRemove(
			Entity entity, 
			object context, 
			Exception exception)
		{ OnEnterpriseRemove(entity, context, exception); }
		
		public void ReceiveRPCCall(RPCEventArgs args)
		{
      if (args.HasResults)
      {
				enterpriseList.Fill(args.Payload, null);
				SetStatus(string.Format("{0} enterprises returned from search.", 
                                args.Payload.Count()));
			}
		} // end SearchResultCallback
		
		public string Status 
		{
		  set
		  {
		    if (status != null)
		    {
		      status.Markup = value;
		    }
		  }
		}
		
		public void SetStatus(string message)
		{
		  Status = message;
		}
		
		public Gtk.Label StatusLabel
		{
		  set { status = value; }
		}

    public void Remove(Entity entity)
    {
      enterpriseList.Remove(entity);
    } // end Remove
  }
}
